﻿/*
#------------------------------------------------------------------------------
#-- Program Name:	[dbo].[spBkp_GetLogBackupName]
#-- Purpose:		Gets the full file name/path for a transaction log backup.
#--	Last Update:	12/21/2016
#--					For a complete history - please review comments in Version
#--					Control.
#-- Called By:		[dbo].[spBkp_Log]
#------------------------------------------------------------------------------
*/
CREATE PROCEDURE [dbo].[spBkp_GetLogBackupName]
(
	@database_name				sysname,
	@backup_directory_name		varchar(8000)		OUTPUT
)
AS
BEGIN
	--- Declare Local Variables
	DECLARE	@dated_backup varchar(8000), @file_name varchar(8000), @iCount int

	--- Determine the Backup Part names
	EXEC	[dbo].[spBkp_GetDirectory]	@database_name = @database_name, 
										@backup_directory_name = @backup_directory_name OUTPUT	
	SELECT	@dated_backup = REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(varchar(50), GETDATE(), 121), '-', ''), ':' , ''), ' ' , ''), '.', '_'),
			@file_name = @database_name + '_tlog_' + @dated_backup + '.trn'

	--- If the file already exists, add an increment counter to the file name
	WHILE [dbo].[fnIO_FileExists](@backup_directory_name + @file_name) = 1
	BEGIN
		--- File Counter Incremental Number
		IF @iCount IS NULL
			SET	@iCount = 1
		ELSE
			SET	@iCount = @iCount + 1

		--- New file name including the incremental counter
		SET	@file_name = @database_name + '_tlog_' + @dated_backup + '_' + CAST(@iCount as varchar(1000)) + '.trn'
	END

	--- Return the name of the Backup File
	SELECT	@backup_directory_name = @backup_directory_name + @file_name
END
